var has_sign = 0;
$(document).ready(function(){
    if(has_sign>0){
        location.href=ctx+'/courier/courierIndex.do'
    }
});

//增加签收数量
function plusNum(obj) {

    var goods_no = $(obj).attr('goods_no');

    var goods_num = $('#num_'+goods_no).val();

    var show_goods_num = parseInt($('#show_num_'+goods_no).html());

    show_goods_num++;

    if(show_goods_num>goods_num){
        alertTips("不能超过下单最大数量！");
        return false;
    }

    var unit_price = parseFloat($('#unit_price_'+goods_no).val());


    var goods_price = parseFloat(show_goods_num)*unit_price;

    //显示单个商品总价
    $('#show_price_'+goods_no).html(goods_price);

    //更改最终商品数量
    $('#final_num_'+goods_no).val(show_goods_num);

    $('#show_num_'+goods_no).html(show_goods_num);

    //更改订单总价
    var show_order_price = parseFloat($('#show_order_price').html());

    show_order_price = show_order_price+unit_price;

    $('#show_order_price').html(show_order_price.toFixed(2));
}

//减少签收数量
function subNum(obj) {

    var goods_no = $(obj).attr('goods_no');

    var show_goods_num = parseInt($('#show_num_'+goods_no).html());

    show_goods_num--;

    if(show_goods_num<0){
        alertTips("签收数量不能小于0！");
        return false;
    }

    var unit_price = parseFloat($('#unit_price_'+goods_no).val());

    var goods_price = parseFloat(show_goods_num)*unit_price;

    //显示单个商品总价
    $('#show_price_'+goods_no).html(goods_price);

    //更改最终商品数量
    $('#final_num_'+goods_no).val(show_goods_num);

    $('#show_num_'+goods_no).html(show_goods_num);

    //更改订单总价
    var show_order_price = parseFloat($('#show_order_price').html());

    show_order_price = show_order_price-unit_price;

    $('#show_order_price').html(show_order_price.toFixed(2));
}


//开始签收
function doSign() {

    if(has_sign<1){
        var final_goods_array = new Array();

        for(var i=0;i<goods_no_array.length;i++){
            var goods_no = goods_no_array[i];
            var goods_num_final = $('#final_num_'+goods_no).val();
            final_goods_array.push(new finalGoods(goods_no,goods_num_final))
        }

        $.ajax({
            type: "post",
            url: '/courier/doSignOrder.do',
            dataType: "json",
            data: {
                'order_no':$('#order_no').val(),
                'scan_check_code':$('#order_check_code').val(),
                'goods_final_json':JSON.stringify(final_goods_array)
            },
            success: function(resp){
                has_sign = 1;
                console.log(resp);
                if(resp.result==="success"){
                    location.href=ctx+'/courier/toSignFinished.do'
                }else{
                    alertTips(resp.msg);
                }
            }
        });
    }else{
        alertTips("请不要重复签收！");
    }

}

function finalGoods(goods_no,goods_num_final) {
    this.goods_no = goods_no;
    this.goods_num_final = goods_num_final;
}